$(function () {

    var setting = {
        view: {
            addHoverDom: addHoverDom,
            removeHoverDom: removeHoverDom,
        },
        edit: {
            enable: true
        },
        async: {
            enable: true,
            url: "dept/getDeptDataDetails",//url地址
            type: 'post',
            autoParam: ["id"],
        },
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "pid",
                rootPId: 0
            }
        },
        callback: {
            beforeRemove: beforeRemove,//点击删除时触发，用来提示用户是否确定删除（可以根据返回值 true|false 确定是否可以删除）
            beforeEditName: beforeEditName,//点击编辑时触发，用来判断该节点是否能编辑
            beforeRename: beforeRename,//编辑结束时触发，用来验证输入的数据是否符合要求(也是根据返回值 true|false 确定是否可以编辑完成)
            onRemove: onRemove,//(beforeRemove返回true之后可以进行onRemove)删除节点后触发，用户后台操作
            onRename: onRename,//编辑后触发，用于操作后台
            onAsyncError: zTreeOnAsyncError,
            onAsyncSuccess: function (event, treeId, treeNode, msg) {

            }
        }
    };

//删除
    function beforeRemove(treeId, treeNode) {
        if (confirm("确认删除?\n将会删除下面的所有机构！")) {
            if (treeNode.isParent) {
                alert("该目录下面还有子目录，请从最底层目录开始删除!");
                return false;
            }
            return true;
        }
        return false;
    }

    function onRemove(e, treeId, treeNode) {
        var deptId = treeNode.id;
        $.post("/dept/removeDept",
            {"deptId": deptId},
            function (repsonse) {
                alert(repsonse.msg);
            }
        )


    }

// 编辑
    function beforeEditName(treeId, treeNode) {
        /* if(treeNode.isParent){
            alert("不准编辑非叶子节点！");
            return false;
        } */
        return true;
    }

    function beforeRename(treeId, treeNode, newName, isCancel) {
        if (newName.length < 3) {
            alert("名称不能少于3个字符！");
            return false;
        }
        return true;
    }

    function onRename(e, treeId, treeNode, isCancel) {
        if (confirm("您确认修改类别名称?")) {
            $.post("/dept/alterDept",
                {
                    "deptId": treeNode.id,
                    "deptName": treeNode.typeName
                },
                function (response) {
                    if (0 == response.code) {

                    } else {
                        alert(response.msg);
                    }
                });
        }
    }

//添加节点

    function addHoverDom(treeId, treeNode) {
        var sObj = $("#" + treeNode.tId + "_span");
        if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0)
            return;
        var addStr = "<span class='button add' id='addBtn_" + treeNode.tId + "' title='添加子节点' onfocus='this.blur();'></span>";
        sObj.after(addStr);
        var btn = $("#addBtn_" + treeNode.tId);
        if (btn) btn.bind("click", function () {
            if (confirm("确认在该目录下面添加此结构?")) {
                var typeName = prompt("请输入类别名称");//获取到的名字
                if (typeName != null) {//点击确定
                    if (typeName.length > 1) {
                        var upId = treeNode.id;//上级编号
                        $.post("/dept/insertDept",
                            {
                                upId: upId,
                                deptName: typeName
                            },
                            function (response) {
                                if (response.code == 0) {
                                    alert(response.msg);
                                }
                                if (response.code == 0) {
                                    window.location.reload();
                                }
                            });
                    } else {
                        alert("请输入正确的类别名称")
                    }
                }
            }
            return false;
        });
    }

    function zTreeOnAsyncError(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
        alert("加载错误：" + XMLHttpRequest);
    }

    function removeHoverDom(treeId, treeNode) {
        $("#addBtn_" + treeNode.tId).unbind().remove();
    };

    $(document).ready(function () {
        $.fn.zTree.init($("#tree"), setting);
    });
})